package com.example.demo31;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;

public class FunctionActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_function);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

    public void innerWrite(View view) throws Exception {
        EditText editTextInput = findViewById(R.id.editTextInput);//获取想要输入的文本
        //获取用户输入
        String userInput = editTextInput.getText().toString();

        // 验证输入长度
        if (userInput.length() > 20) {
            Toast.makeText(this, "输入过长，请限制在20个字符以内", Toast.LENGTH_SHORT).show();  // 提示用户输入过长
            editTextInput.setText("");  // 清空输入框内容
        } else if (!userInput.isEmpty()) {
            // 写入文件操作
            FileOutputStream fos = openFileOutput("inner.txt", MODE_PRIVATE);
            fos.write(userInput.getBytes(StandardCharsets.UTF_8));
            fos.close();  // 关闭文件输出流
            Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();  // 显示成功消息

            // 清空输入框内容
            editTextInput.setText("");  // 将输入框内容设置为空字符串
        } else {
            Toast.makeText(this, "请输入一些内容", Toast.LENGTH_SHORT).show();  // 提示用户输入内容
        }
    }

    public void innerRead(View view) throws Exception{
        FileInputStream fis= openFileInput("inner.txt");//读取数据
        //首先判断输入的数据的长度
        byte[] buffer = new byte[fis.available()];
        //byte[] 是在许多编程语言（如 Java 和 C#）中表示字节数组的语法。
        // 字节数组用于存储二进制数据，可以是字符串、图像、音频等各种类型的信息。
        //创建字节数组 byte[] bytes = new byte[10]; // 创建一个长度为10的字节数组

        //将数据读到buffer容器中
        fis.read(buffer);
        fis.close();
        Toast.makeText(this,new String(buffer),Toast.LENGTH_SHORT).show();  //操作完成后，输出相应的信
    }

    public void outerWriting(View view) throws Exception
    {
        //先判断外部存储是否挂载中
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED))
        {
            //写入数据
            File path = Environment.getExternalStorageDirectory();
            File file = new File(path,"out.txt");
            FileOutputStream fos = new FileOutputStream(file);
            fos.write("hello!".getBytes());
            fos.close();
            Toast.makeText(this, "写入成功", Toast.LENGTH_SHORT).show();

        }

    }

    public void outerRead(View view) throws Exception {
        String state = Environment.getExternalStorageState();
        if (state.equals(Environment.MEDIA_MOUNTED)) {
            //写入数据
            File path = Environment.getExternalStorageDirectory();
            File file = new File(path, "outer.txt");
            FileInputStream fos = new FileInputStream(file);
            byte[] buffer = new byte[fos.available()];
            fos.read(buffer);
            fos.close();
            Toast.makeText(this, new String(buffer), Toast.LENGTH_SHORT).show();

        }
    }



    public void todetail(View view) {
        Intent intent = new Intent(this, DetailActivity.class);
        startActivity(intent);
    }

    public void backmain(View view) {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);


    }


}